package org.sunsys.spitterapp.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class SpitterHomePageController {

	public SpitterHomePageController() {
	}
	
	/**
	 * 
	 * URL: http://<hostname>[:<port>]/<context>/<dispatcher-redirection-pattern>/home	
	 * 		or	
	 * 		http://<hostname>[:<port>]/<context>/spitter/
	 * 
	 * e.g.:
	 * 		http://localhost:8080/spitterapp/spitter/home
	 * 		http://localhost:8080/spitterapp/spitter/
	 * 		assumes
	 * 		======= 
	 * 			spitter			dispatcher servlet redirection pattern which is configured in web.xml
	 * 			localhost 		deployed in local host with http port as 8080
	 * 			spitterapp		is the application context name
	 *  
	 * @return String
	 */
	@RequestMapping(value={"/", "/home"}, method=RequestMethod.GET)
	public String showHomePage() {
		System.out.println("show Home Page method");
		return "redirect:/HomePage.html";
	}
	
	/**
	 * URL: http://<hostname>[:<port>]/<context>/<dispatcher-redirection-pattern>/homePage?classicHomePage=true	
	 * e.g.:
	 * 		http://localhost:8080/spitterapp/spitter/homePage?classicHomePage=true
	 * 		assumes
	 * 		======= 
	 * 			spitter			dispatcher servlet redirection pattern which is configured in web.xml
	 * 			localhost 		deployed in local host with http port as 8080
	 * 			spitterapp		is the application context name
	 * 
	 * narrow the request matching through parameters condition.  if classicHomePage=false is false in URL request, then this method won't be matched
	 * 
	 * @return String
	 */
	@RequestMapping(value="/homePage", method=RequestMethod.GET, params="classicHomePage=true")
	public String showClassicHomePage() {
		System.out.println("Clasic HomePage method");
		return "redirect:/ClassicHomePage.html";
	}
	
	/**
	 * URL: http://<hostname>[:<port>]/<context>/<dispatcher-redirection-pattern>/homePage	
	 * e.g.:
	 * 		http://localhost:8080/spitterapp/spitter/homePage
	 * 		http://localhost:8080/spitterapp/spitter/homePage?var1=value1
	 * 		assumes
	 * 		======= 
	 * 			spitter			dispatcher servlet redirection pattern which is configured in web.xml
	 * 			localhost 		deployed in local host with http port as 8080
	 * 			spitterapp		is the application context name
	 * 
	 * narrow the request matching through parameters condition.  if classicHomePage parameter is not present in URL request, then this method won't be matched
	 * 
	 * @return String
	 */
	@RequestMapping(value="/homePage", method=RequestMethod.GET, params="!classicHomePage")
	public String showNextGenHomePage() {
		System.out.println("NextGen Home Page method");
		return "redirect:/NextGenHomePage.html";
	}
	
	/**
	 * URL: http://<hostname>[:<port>]/<context>/<dispatcher-redirection-pattern>/home?new
	 * e.g.: http://localhost:8080/spitterapp/spitter/home?new
	 * 
	 * narrow the request matching through parameters. if "new" parameter is present in the URL (any where in the URL), then only this method gets matched
	 * 
	 * 		assumes
	 * 		======= 
	 * 			spitter			dispatcher servlet redirection pattern which is configured in web.xml
	 * 			localhost 		deployed in local host with http port as 8080
	 * 			spitterapp		is the application context name
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/home", method=RequestMethod.GET, params="new")
	public String showNewHomepage() {
		System.out.println("show New Homepage method");
		return "redirect:/NewHomePage.html";
	}
	
	/**
	 * URL: http://<hostname>[:<port>]/<context>/<dispatcher-redirection-pattern>/homePageWithHeaders
	 * e.g.: http://localhost:8080/spitterapp/spitter/homePageWithHeaders
	 * 
	 *	this method gets matched, if "heaserVar1" is present in the request header
	 * 		assumes
	 * 		======= 
	 * 			spitter			dispatcher servlet redirection pattern which is configured in web.xml
	 * 			localhost 		deployed in local host with http port as 8080
	 * 			spitterapp		is the application context name
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/homePageWithHeaders", method=RequestMethod.GET, headers="heaserVar1")
	public String showHomePageWithHeaders() {
		System.out.println("Home Page With Headers method");
		return "redirect:/HomePageWithHeaders.html";
	}
	
	/**
	 * URL: http://<hostname>[:<port>]/<context>/<dispatcher-redirection-pattern>/homePageWithUserName/userName/{username}
	 * e.g.: http://localhost:8080/spitterapp/spitter/homePageWithUserName/userName/ravi
	 * 
	 *  in the above example, ravi is the user name, which will be assigned to the userName String variable. 
	 *  @PathVariable(value="username") is used to specify explicitly   
	 * 
	 * 		assumes
	 * 		======= 
	 * 			spitter			dispatcher servlet redirection pattern which is configured in web.xml
	 * 			localhost 		deployed in local host with http port as 8080
	 * 			spitterapp		is the application context name
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/homePageWithUserName/userName/{username}", method=RequestMethod.GET)
	public String showHomePageWithUserName(@PathVariable(value="username") String userName) {
		System.out.println("user Name:"+userName);
		System.out.println("HomePage With User Name method");
		return "redirect:/HomePageWithUserName.html";
	}
	
	/**
	 * URL: http://<hostname>[:<port>]/<context>/<dispatcher-redirection-pattern>/homePageWithUserName/userName/{username}/userid/{userid}
	 * e.g.: http://localhost:8080/spitterapp/spitter/homePageWithUserName/userName/ravi/userid/ravi17
	 * 
	 * 	in the above example, ravi is the user name and ravi17 is userId, will be assigned to the userName and userId variables respectively. 
	 * 	@PathVariable(value="username") is used to specify explicitly 
	 *  in case of userId,  @PathVariable is not explicitly specified the userId. looks for String variable name equivalent name (  /userid/"{userId}" )in the URL     
	 *  
	 * 		assumes
	 * 		======= 
	 * 			spitter			dispatcher servlet redirection pattern which is configured in web.xml
	 * 			localhost 		deployed in local host with http port as 8080
	 * 			spitterapp		is the application context name
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/homePageWithUserName/userName/{username}/userid/{userId}", method=RequestMethod.GET)
	public String showHomePageWithUserName(@PathVariable(value="username") String userName, @PathVariable String userId, 
				@RequestParam(value="nickName", required=false, defaultValue="No NickName") String nickName) {
		System.out.println("user Name:"+userName);
		System.out.println("user id:"+userId);
		System.out.println("HomePage With User Name method");
		return "redirect:/HomePageWithUserName.html";
	}

	/**
	 * URL: http://<hostname>[:<port>]/<context>/<dispatcher-redirection-pattern>/homeWithMediaType
	 * 
	 * e.g.: http://localhost:8080/spitterapp/spitter/homeWithMediaType
	 * 
	 * usually media type can be used when we are sending request with some pay load of specific type
	 * 
	 * 		assumes
	 * 		======= 
	 * 			spitter			dispatcher servlet redirection pattern which is configured in web.xml
	 * 			localhost 		deployed in local host with http port as 8080
	 * 			spitterapp		is the application context name
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value="homeWithMediaType", method=RequestMethod.GET, consumes={"text/html", "application/xml"})
	public String showHomePageXmlAcceptableMediaType(){
		System.out.println("HomePage With User Name method");
		return "redirect:/HomePageXmlAcceptableMediaType.html";
	}
	
}
